package com.bkhech.home.practice.spring.lifecycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;

import javax.annotation.PostConstruct;

/*
 * @Component方式注入
 * @author guowm
 * @date 2021/5/14
 */
@Component
public class LifecycleDemo3 implements
        BeanNameAware,
        BeanClassLoaderAware,
        BeanFactoryAware,
        EnvironmentAware,
        InitializingBean,
        SmartInitializingSingleton,
        DisposableBean,
        ApplicationContextAware {

    private String name = "LifecycleDemo3";

    static {
        System.out.println("LifecycleDemo3 static");
    }

    {
        System.out.println("LifecycleDemo3 block");
    }

    public LifecycleDemo3() {
        this.name = "bkhech";
        System.out.println("LifecycleDemo3 constructor");
    }

    @PostConstruct
    public void initPostConstruct() {
        System.out.println("LifecycleDemo3 @PostConstruct");
    }

    public void initMethod() {
        System.out.println("LifecycleDemo3 initMethod");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("LifecycleDemo3 setBeanFactory");
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("LifecycleDemo3 setBeanName---" + name);
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("LifecycleDemo3 destroy");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("LifecycleDemo3 afterPropertiesSet");
    }

    @Override
    public void afterSingletonsInstantiated() {
        //实例化之后执行
        System.out.println("LifecycleDemo3 afterSingletonsInstantiated");
    }

    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        System.out.println("LifecycleDemo3 setBeanClassLoader");
    }

    /**
     * Set the {@code Environment} that this component runs in.
     *
     * @param environment
     */
    @Override
    public void setEnvironment(Environment environment) {
        System.out.println("LifecycleDemo3 environment");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("LifecycleDemo3 setApplicationContext");
    }
}
